package com.system.goods.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.common.bean.Address;
import com.system.common.bean.Goods;
import com.system.common.bean.SeckillGoods;
import com.system.common.bean.Type;
import com.system.common.exception.ServiceException;
import com.system.common.service.IGoodsService;
import com.system.common.service.ITypeService;
import com.system.goods.mapper.GoodsMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @DubboReference
    private ITypeService typeService;
    @Override
    public Goods getGoodsById(Integer id) {
        return super.getById(id);
    }
   @Override
   public Goods getGoodsByName(String name) {

        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getName, name);
        return super.getOne(queryWrapper);
    }


    @Override
    public void addGoods(Goods goods) {
        super.save(goods);
    }
  @Override
    public void updateGoods(Goods goods) {
        super.updateById(goods);
    }

    @Override
    public void deleteGoods(Integer id) {
        Goods goods = super.getById(id);
        if (goods == null) {
            throw new ServiceException("商品不存在");
        }
        if(goods.getIsdel()==1){
            throw new ServiceException("商品已删除");
        }
        super.removeById(id);
    }
@Override
public void updateStatus(Integer id, Integer status){
        Goods goods=super.getById(id);
        if(goods==null){
            throw new ServiceException("商品不存在");
        }
        if(goods.getIsdel()==1){
            throw new ServiceException("商品已删除");
        }
        goods.setStatus(status);
        super.updateById(goods);
    }


@Override
public List<Goods> getGoodsList() {
        return super.list();
    }

  @Override
  public Page<Goods> getPage(Goods goods, Page<Goods> page) {
        // 构建查询条件
      LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();

      if (goods.getName() != null && !goods.getName().isEmpty()){
          queryWrapper.like(Goods::getName, goods.getName());
      }
      if (goods.getTypeid() != null){
          queryWrapper.eq(Goods::getTypeid, goods.getTypeid());
      }
      if (goods.getStatus() != null){
          queryWrapper.eq(Goods::getStatus, goods.getStatus());
      }
      Page<Goods> resultPage = baseMapper.selectPage(page, queryWrapper);

      List<Goods> records = resultPage.getRecords();
      for (Goods good : records) {
          if (good.getTypeid() != null){
              Type t = typeService.getTypeById(good.getTypeid());
              good.setType(t);
          }
      }

      Long total = this.count(queryWrapper);
      resultPage.setTotal(total);
      return resultPage;
  }

    @Override
    public void reduceStock(Integer id, Integer count) {
        Goods good = super.getById(id);
        if (good == null || good.getIsdel() == 1) {
            throw new ServiceException("商品不存在或已删除");
        }
        if (good.getStock() < count) {
            throw new ServiceException("商品库存不足");
        }
        good.setStock(good.getStock() - count);
        super.updateById(good);
    }


}
